package com.zl.controller;

import com.zl.beans.MeetRoom;
import com.zl.beans.Page;
import com.zl.service.MeetRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class MeetRoomController {
    @Autowired
    private MeetRoomService roomService;
    /*分页查询*/
    @RequestMapping("/roomManage")
    public String roomManage(@RequestParam(defaultValue = "1") int pagenum,boolean flag, Model model){
        Page<MeetRoom> rooms = roomService.findRoom(pagenum);
        for (MeetRoom ns:rooms.getDatas()){
            System.out.println(ns);
        }
        model.addAttribute("rooms",rooms);
        return "/html/meeting/meetRoomManage.jsp";
    }
    /*按会议室名字查询*/
    @RequestMapping("/selectRoomName")
    public String selectRoomName(String name, Model model){
        Page<MeetRoom> page = roomService.findRoomByName(name);
        for (MeetRoom ns:page.getDatas()){/*测试输出*/
            System.out.println(ns);
        }
        model.addAttribute("rooms",page);

        return "/html/meeting/meetRoomManage.jsp";
    }
    /*添加会议室*/
    @RequestMapping("/testSubmit")
    public String testSubmit(@RequestParam("uploadFile")MultipartFile file,
                             String roomName, String roomContent)throws
            IllegalStateException, IOException {
        //上传文件存储路径，即D\tmp文件夹
        String filePath = ("D://tmp");
        if(!file.isEmpty()) {
            //上传文件的全路径
            File tempFile = new File(filePath+"/"+file.getOriginalFilename());
            //文件上传
            file.transferTo(tempFile);
        }
        System.out.println(roomName+"88888"+ roomContent);
        System.out.println("执行了……");
        roomService.addRoom(roomName,roomContent);
        return "redirect:/roomManage";

    }
    /*修改会议室*/
    @RequestMapping("/updateRoom")
    public String updateRoom(String roomName, String roomContent,Integer flag,Integer id,Model model){

        if(flag==1){
            MeetRoom room = roomService.selectById(id);
            System.out.println(room);
            model.addAttribute("room",room);
            return "/html/meeting/changeRoom.jsp";
        }else{
            System.out.println("俺来了");
            roomService.changeRoom(roomName,roomContent,id);
            return "redirect:/roomManage";
        }
    }

    /*删除会议室*/
    @RequestMapping("/delectRoom")
    public String delectRoom(Integer id){
        roomService.rmRoom(id);
        return "redirect:/roomManage";
    }
    @RequestMapping("/test02")
    public String test02(){
        System.out.println("俺来了");
        return "null";
    }
}
